<?php
namespace app\Capital\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Model;
use think\Db;

class Membercapital extends Admin
{
    public function index()
    {
        return ZBuilder::make('table')
            ->setPageTitle('会员资金管理')
            ->fetch();
    }

    //会员充值单据列表
    public function Chargelist()
    {
        $where = [];
        $startDate = input('_filter_time_from');
        $endDate = input('_filter_time_to');
        if ($startDate && $endDate) {
            $startDate = strtotime($startDate . ' 0:00:00');
            $endDate = strtotime($endDate . " 23:59:59");
            $where['CreateTime'] = ['between', [$startDate, $endDate]];
        }
        $searche_field = input('search_field');
        $keyword = input('keyword');
        if ($searche_field && $keyword) {
            switch ($searche_field) {
                case 'ID':
                    $where['MemberID'] = $keyword;
                    break;
                case 'Name':
                    $towhere['Name'] = ['like', "%$keyword%"];
                    $Member = Db::name('Member')->where($towhere)->column('ID');
                    $MemberStr = implode(",", $Member);
                    $where['MemberID'] = ['in', $MemberStr];
                    break;
                case 'No':
                    $where['No'] = $keyword;
                    break;
                default:
                    $where['No'] = $keyword;
            }
        }
        $status = input('status');
        if ($status) {
            $where['status'] = 0;
        }
        if ($where) {
            $data = Db::name("Member_chargelist")->where($where)->order("ID desc")->paginate(5);
        } else {
            $data = Db::name("Member_chargelist")->order("ID desc")->paginate(5);
        }
        // 分页数据
        $page = $data->render();
        //充值原因
        $msg=Db::name('item_type')->field("id,Name")->where('type=7')->select();
        $NewMsg=[];
        foreach($msg as $value){
            $NewMsg[$value['id']]=$value['Name'];
        }
        $MemberArray = [10053 => '吉尔丹', 12767 => '长沙好迪', 12795 => '长沙泰格'];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('充值单列表')// 设置页面标题
            ->setPrimarykey('ID')
            ->setSearch(['ID' => 'ID', 'Name' => '客户名称', 'No' => '单据号'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['ID', 'ID'],
                ['No', '单据号'],
                ['MemberID', '客户名称', '', '', $MemberArray],
                ['Num', '充值金额'],
                ['CreateTime', '创建时间', 'datetime']
            ])
            ->addColumn('Type', '原因', 'type', '', $NewMsg)
            ->addColumn('status', '状态', 'status', '', [0 => '待审核', 1 => '已审核', 2 => '已作废'])
            ->addColumn('right_button', '操作', 'btn')
            ->addTopButton('add')// 批量添加顶部按钮
            ->addTopButton('enable', ['title' => '批量审核', 'table' => 'Member_chargelist'])
            ->addTopButton('custom', ['title' => '待审核', 'icon' => 'fa fa-fw fa-cogs', 'href' => '/admin.php/Capital/Membercapital/Chargelist.html?status=10'])
            ->addTimeFilter('createtime')
            ->addRightButton('edit', ['title' => '作废', 'icon' => 'fa fa-wrench'])// 批量添加右侧按钮
            ->setRowList($data)
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }

    //充值
    public function add()
    {
        $msg=Db::name('item_type')->where('type=7')->column('Name');
        return ZBuilder::make('form')
            ->setPageTitle('新建充值单')
            ->addSelect('MemberID', '会员', '', [10053 => '吉尔丹', 12767 => '长沙好迪', 12795 => '长沙泰格'])
            ->addText('Num', '充值金额', '', '', '', 'onkeyup="if(isNaN(value))execCommand(\'undo\')" onafterpaste="if(isNaN(value))execCommand(\'undo\')"', '')
            ->addSelect('Type', '充值原因', '', $msg)
            ->layout(['MemberID' => 4, 'Num' => 4, 'Type' => 4])
            ->setUrl(url('save'))
            ->fetch();
    }

    //保存充值单据
    public function save()
    {
        $data = input();
        $No = date('YmdHis');
        $data['CreateTime'] = time();
        $data['No'] = 'CZ' . $data['MemberID'] . $No;
        $result = Db::name("member_chargelist")->insert($data);
        if ($result) {
            return $this->success("充值成功", url('Chargelist'));
        } else {
            return $this->error("充值失败", url('add'));
        }
    }
}